home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1995 Fall / PD-ROM F95.toast / Programming / Programming Utilities / PICTButton CDEF 1.0.7 ƒ / PICTButton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-03  |  5.5 KB  |  250 lines  |  [TEXT/ttxt]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     PICTButton CDEF
  4.     version 1.0.7
  5.     3 July 1994
  6.     
  7.     Written by: Paul Celestin
  8.     
  9.     This CDEF displays a picture whose resource ID is derived from
  10.     the value field of the CNTL. The min, max, and refcon fields are
  11.     not used and should therefore not be set.
  12.     
  13.     940703 - 1.0.7    now works over multiple monitors of different bit
  14.                     depths
  15.  
  16.     940319 - 1.0.6    removed unnecessary erasing code
  17.     
  18.     931012 - 1.0.5    really fixed problem with black and white PICTs
  19.                     showing up properly
  20.     
  21.     931012 - 1.0.4    changed color of text in variation 2 to blue
  22.     
  23.     930922 - 1.0.3    fixed a problem with black and white PICTs showing
  24.                     up, or so I thought
  25.     
  26.     930915 - 1.0.2    fixed the new variations
  27.     
  28.     930829 - 1.0.1     added variations 1 and 2
  29.     
  30.     930822 - 1.0.0    initial release
  31.  
  32. ---------------------------------------------------------------------- */
  33.  
  34. # include    <Controls.h>
  35. # include    <QuickDraw.h>
  36. # include    <Resources.h>
  37. # include    <Traps.h>
  38.  
  39. # define    DEFAULTPICT        0
  40. # define    INVERTEDPICT    1
  41. # define    BW_OFFSET        2
  42.  
  43. /* ----------------------------------------------------------------------
  44. prototypes
  45. ---------------------------------------------------------------------- */
  46.  
  47. pascal     long        main(short, ControlHandle, short, long);
  48.         int             hasColor(Rect);
  49.          void         drawIt(ControlHandle, short);
  50.         long         testIt(ControlHandle, Point);
  51.  
  52. /* ----------------------------------------------------------------------
  53. main - here is where it all began...
  54. ---------------------------------------------------------------------- */
  55. pascal long main
  56.     (
  57.     short            variation,
  58.     ControlHandle    theControl,
  59.     short            message,
  60.     long            param
  61.     )
  62.  
  63. {
  64.     long            returnValue = 0L;
  65.     char            state = HGetState((Handle)theControl);
  66.  
  67.     switch(message)
  68.     {
  69.         case drawCntl:
  70.             drawIt(theControl,variation);
  71.         case testCntl:
  72.             returnValue = testIt(theControl, *(Point *) ¶m);
  73.         case calcCRgns:
  74.             break;
  75.           case initCntl:
  76.               break;
  77.         case dispCntl:
  78.             break;
  79.         case posCntl:
  80.             break;
  81.         case thumbCntl:
  82.             break;
  83.         case dragCntl:
  84.             break;
  85.         case autoTrack:
  86.             break;
  87.         case calcCntlRgn:
  88.             break;
  89.         case calcThumbRgn:
  90.             break;
  91.         default:
  92.             break;
  93.     }
  94.  
  95.     HSetState((Handle)theControl,state);
  96.  
  97.     return(returnValue);                /* tell them what happened */
  98. }
  99.  
  100.  
  101. /* ----------------------------------------------------------------------
  102. hasColor
  103. ---------------------------------------------------------------------- */
  104. int hasColor(Rect r)
  105.  
  106. {
  107.     SysEnvRec        myComputer;
  108.     GDHandle        curDev;
  109.     PixMapHandle    myPixMap;
  110.     
  111.     SysEnvirons(2,&myComputer);
  112.     if (myComputer.hasColorQD)
  113.     {
  114.         LocalToGlobal((Point*) &r);
  115.         LocalToGlobal(1 + (Point*) &r);
  116.         curDev = GetMaxDevice(&r);
  117.         myPixMap = (**curDev).gdPMap;
  118.         if ((**myPixMap).pixelSize > 1)
  119.             return(1);
  120.         else
  121.             return(0);
  122.     }
  123.     return(0);
  124. }
  125.  
  126.  
  127. /* ----------------------------------------------------------------------
  128. drawIt - here is where we actually draw the control
  129. ---------------------------------------------------------------------- */
  130. static void drawIt
  131.     (
  132.     ControlHandle    control,
  133.     short            variation
  134.     )
  135.  
  136. {
  137.     Rect                 myRect;
  138.     short                 myPictID;
  139.     GrafPtr                myPort;
  140.     PicHandle            myPicture;
  141.     PenState            oldPenState;
  142.     Str255                myTitle;
  143.     int                    savedFont,
  144.                         savedSize,
  145.                         savedMode;
  146.     Pattern                myGray;
  147.  
  148.     GetPort(&myPort);                        /* save off the current port */
  149.  
  150.     if (!(*control)->contrlVis)                /* if not visible, do nothing */
  151.         return;
  152.  
  153.     myPictID = GetCtlValue(control);        /* base ID is stored in the value field */
  154.         
  155.     myRect = (*control)->contrlRect;
  156.     
  157.     if (!hasColor(myRect))                    /* use black and white PICTs */
  158.         myPictID = myPictID + BW_OFFSET;
  159.     
  160.     if ((*control)->contrlHilite == inButton)
  161.         myPictID = myPictID + INVERTEDPICT;    /* invert while tracking */
  162.     
  163.     myPicture = (PicHandle)GetResource('PICT', myPictID);
  164.     
  165.     if ( myPicture == 0L )                    /* could not find the PICT */
  166.         return;
  167.  
  168.     LoadResource((Handle)myPicture);
  169.     HNoPurge((Handle)myPicture);
  170.  
  171.     DrawPicture(myPicture, &myRect);        /* draw the picture */
  172.     
  173.     switch (variation)
  174.     {
  175.     case 1:                                    /* display title of control in geneva 9 */
  176.         {
  177.             savedFont = myPort->txFont;        /* save off current values */
  178.             savedSize = myPort->txSize;
  179.             savedMode = myPort->txMode;
  180.             
  181.             ForeColor(blueColor);
  182.             TextFont(geneva);
  183.             TextSize(9);
  184.             TextMode(srcOr);
  185.             
  186.             BlockMove(((*control)->contrlTitle),myTitle,((*control)->contrlTitle)[0] + 1);
  187.             
  188.             MoveTo((myRect.right+myRect.left) / 2 - StringWidth(myTitle) / 2
  189.                 ,myRect.bottom - 6);
  190.             DrawString(myTitle);
  191.             
  192.             TextFont(savedFont);            /* restore saved values */
  193.             TextSize(savedSize);
  194.             TextMode(savedMode);
  195.             ForeColor(blackColor);
  196.             
  197.             break;
  198.         }
  199.     case 2:                                    /* simple 2-pixel wide black border */
  200.         {
  201.             PenSize(2,2);
  202.             InsetRect(&myRect,-3,-3);
  203.             FrameRect(&myRect);
  204.             InsetRect(&myRect,3,3);
  205.             PenSize(1,1);
  206.             break;
  207.         }
  208.     }
  209.  
  210.     if ((*control)->contrlHilite == 255)    /* gray out the picture */
  211.     {
  212.         GetPenState(&oldPenState);
  213.         PenNormal();
  214.         GetIndPattern(&myGray,0,4);
  215.         PenPat(&myGray);
  216.         PenMode(patBic);
  217.         PaintRect(&myRect);
  218.         SetPenState(&oldPenState);
  219.     }
  220.     
  221.     HPurge((Handle)myPicture);
  222.     
  223.     return;                                    /* we are done drawing */
  224. }
  225.  
  226.  
  227. /* ----------------------------------------------------------------------
  228. testIt - test for mouse hits within control
  229. ---------------------------------------------------------------------- */
  230. static long testIt
  231.     (
  232.     ControlHandle        control,
  233.     Point                myPoint
  234.     )
  235.  
  236. {
  237.     Rect myRect;
  238.  
  239.     myRect = (*control)->contrlRect;
  240.     
  241.     if    (
  242.         ((*control)->contrlVis != 0) &&
  243.         ((*control)->contrlHilite != 255) &&
  244.         (PtInRect(myPoint,&myRect))
  245.         )
  246.         return(inButton);
  247.     else
  248.         return(0);
  249. }
  250.